package com.example.attempt.controller;

import com.example.attempt.entity.Course;
import com.example.attempt.entity.Student;
import com.example.attempt.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CourseController {
    @Autowired
    private PostServiceImpl postService;
    @Autowired
    private HomeworkServiceImpl homeworkService;
    @Autowired
    private SelectCourseServiceImpl selectCourseService;
    @Autowired
    private CourseServiceImpl courseImpl;
    @Autowired
    private TeacherServiceImpl teacherImpl;
    @PostMapping(path = "/getCourseByTid")
    public List<Course> getCourseByTid(@RequestParam(value = "tid") String tid){
        System.out.println(tid);
        System.out.println(courseImpl.queryByTeacher(tid));
        return courseImpl.queryByTeacher(tid);
    }
    @PostMapping(path = "/getStudentsNotInCourse")
    public List<Student> getStudentNotInCourse(@RequestParam(value = "cid") String cid){
        System.out.println(cid);
        System.out.println(courseImpl.getStudentsNotInCourse(cid));
        return courseImpl.getStudentsNotInCourse(cid);
    }

    @PostMapping(path = "/addCourses")
    public int addCourses(@RequestBody List<Course> courses){
        for (Course c : courses) {
            courseImpl.addCourse(c);
        }
        return 1;
    }

    @PostMapping(path = "/addCourse")
    public int addCourse(@RequestBody Course course){
        List<Course> courses = courseImpl.queryByTeacher(course.getTid());
        List<Course> courses1 = courseImpl.queryByRoom((course.getCroom()));
        if(course.getLearnTime()==32){
            System.out.println("进来32了捏");
            if (course.getCtime().length()==6||course.getCroom().length()==14){
                return -3;
            }
            //老师是否有课
            for (Course c:courses){
                if(c.getCtime().equals(course.getCtime())){
                    return -2;
                }
            }
            //教室是否被使用
            for (Course c:courses1){
                String time1,time2,time;
                time=course.getCtime();
                time1=c.getCtime().substring(0,3);
                time2=c.getCtime().substring(3);
                if(time.equals(time1)||time.equals(time2)){
                    return -2;
                }
            }
        }
        if(course.getLearnTime()==64){
            System.out.println("进来32了捏");
            if (course.getCtime().length()==3||course.getCroom().length()==7){
                return -3;
            }
            String time1,time2;
            time1=course.getCtime().substring(0,3);
            time2=course.getCtime().substring(3);
            String croom=course.getCroom().substring(0,7);
//            course.setCroom(course.getCroom().substring(0,7));
            courses1 = courseImpl.queryByRoom(croom);
            for(Course c:courses){
                System.out.println(c.toString());
                String t1=c.getCtime().substring(0,3);
                String t2=c.getCtime().substring(3);
                if(t1.equals(time1)||t1.equals(time2)||t2.equals(time1)||t2.equals(time2)){
                    return -2;
                }
            }
            for(Course c:courses1){
                String t1,t2;
                t1=c.getCtime().substring(0,3);
                t2=c.getCtime().substring(3);
                if(time1.equals(t1)||time1.equals(t2)||time2.equals(t1)||time2.equals(t2)){
                    return -2;
                }
            }
        }

        System.out.println("进来添加课堂了捏");
        System.out.println(course);
        if(courseImpl.queryById(course.getCid())!=null){
            return 0;//违反cid的主键约束
        }
        if (teacherImpl.queryById(course.getTid())==null){
            return -1;//不存在这个老师
        }
        else {
            courseImpl.addCourse(course);
            return 1;
        }
    }
    @RequestMapping(path = "/getCourse")
    public Course getCourse(@RequestParam(value = "cid") String cid){
        if(courseImpl.queryById(cid)!=null){
            return courseImpl.queryById(cid);
        }
        else{
            Course c = new Course();
            c.setCid("-1");
            return c;
        }
    }
    @PostMapping(path = "/deleteCourse")
    public int deleteCourse(@RequestParam(value = "cid") String cid){
        if(courseImpl.queryById(cid)==null){
            return -3;
        }
        else if(!selectCourseService.queryByCid(cid).isEmpty()){
            return -2;
        }
        else if(!homeworkService.queryByCid(cid).isEmpty()){
            return -1;
        }
        else if(!postService.queryByCid(cid).isEmpty()){
            return 0;
        }

        else{
            courseImpl.deleteCourse(cid);
            return 1;
        }
    }

    //显示所有课堂
    @RequestMapping(path = "/list3")
    public List<Course> getAllCourses(){
        return courseImpl.queryAll();
    }

}
